package com.sk.extension.config;

import com.sk.extension.constants.Constant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    //1. 交换机
    @Bean("ackExchange")
    public Exchange ackExchange(){
        return ExchangeBuilder.topicExchange(Constant.ACK_EXCHANGE_NAME).durable(true).build();
    }
    //2. 队列
    @Bean("ackQueue")
    public Queue ackQueue() {
        return QueueBuilder.durable(Constant.ACK_QUEUE).build();
        //return QueueBuilder.nonDurable().build(); nonDurable设置非持久化
    }
    //3. 队列和交换机绑定 Binding
    //如果使用的是Exchange这个接口, 后面要加上noargs(), 不然会报错
    @Bean("ackBinding")
    public Binding ackBinding(@Qualifier("ackExchange") Exchange exchange,
                              @Qualifier("ackQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("ack").noargs();
    }

//    @Bean("ackBinding")
//    public Binding ackBinding(@Qualifier("ackExchange") DirectExchange exchange,
//                              @Qualifier("ackQueue") Queue queue) {
//        return BindingBuilder.bind(queue).to(exchange).with("ack");
//    }

    //非持久化
    @Bean("presQueue")
    public Queue presQueue(){
        return QueueBuilder.nonDurable(Constant.PRES_QUEUE).build();
    }

    @Bean("presExchange")
    public DirectExchange presExchange(){
        return ExchangeBuilder.directExchange(Constant.PRES_EXCHANGE).durable(false).build();
    }
    @Bean("presBinding")
    public Binding presBinding(@Qualifier("presQueue") Queue queue,
                               @Qualifier("presExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("pres").noargs();
    }

    //发送方确认
    @Bean("confirmQueue")
    public Queue confirmQueue(){
        return QueueBuilder.durable(Constant.CONFIRM_QUEUE).build();
    }
    @Bean("confirmExchange")
    public DirectExchange confirmExchange(){
        return ExchangeBuilder.directExchange(Constant.CONFIRM_EXCHANGE).build();
    }
    @Bean("confirmBinding")
    public Binding confirmBinding(@Qualifier("confirmQueue") Queue queue, @Qualifier("confirmExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("confirm").noargs();
    }

    //重试机制
    @Bean("retryQueue")
    public Queue retryQueue(){
        return QueueBuilder.durable(Constant.RETRY_QUEUE).build();
    }
    @Bean("retryExchange")
    public DirectExchange retryExchange(){
        return ExchangeBuilder.directExchange(Constant.RETRY_EXCHANGE).build();
    }
    @Bean("retryBinding")
    public Binding retryBinding(@Qualifier("retryQueue") Queue queue, @Qualifier("retryExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("retry").noargs();
    }

    //ttl
    //未设置ttl
    @Bean("ttlQueue")
    public Queue ttlQueue(){
        return QueueBuilder.durable(Constant.TTL_QUEUE).build();
    }
    //2. 队列设置ttl
    @Bean("ttlQueue2")
    public Queue ttlQueue2(){
        return QueueBuilder.durable(Constant.TTL_QUEUE2).ttl(20000).build();  //设置队列的ttl为20s
    }

    @Bean("ttlQueue3")
    public Queue ttlQueue3(){
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", 20000);
        return QueueBuilder.durable(Constant.TTL_QUEUE2).withArguments(map).build();  //设置队列的ttl为20s
    }

    @Bean("ttlExchange")
    public DirectExchange ttlExchange(){
        return ExchangeBuilder.directExchange(Constant.TTL_EXCHANGE).build();
    }
    @Bean("ttlBinding")
    public Binding ttlBinding(@Qualifier("ttlQueue") Queue queue, @Qualifier("ttlExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();
    }
    @Bean("ttlBinding2")
    public Binding ttlBinding2(@Qualifier("ttlQueue2") Queue queue, @Qualifier("ttlExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();
    }

    @Bean("transQueue")
    public Queue transQueue(){
        return QueueBuilder.durable(Constant.TRANS_QUEUE).build();
    }



}
